package com.liang.pcc.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.liang.pcc.domain.EquipmentLineInfo;
import com.liang.pcc.excel.BaseExcelListener;
import com.liang.pcc.excel.model.EquipmentLineParam;
import com.liang.pcc.service.EquipmentLineInfoService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 设备/线路参数调研监听器
 */
@Slf4j
public class EquipmentLineParamListener extends BaseExcelListener<EquipmentLineParam> {

    private final EquipmentLineInfoService equipmentLineService;
    private final Long fileId;
    private final Long userId;

    private final List<EquipmentLineInfo> dataList = new ArrayList<>();

    public EquipmentLineParamListener(EquipmentLineInfoService equipmentLineInfoService, CountDownLatch latch, Long fileId, Long userId) {
        super(latch);
        this.equipmentLineService = equipmentLineInfoService;
        this.fileId = fileId;
        this.userId = userId;
    }

    @Override
    public void invoke(EquipmentLineParam equipmentLineParam, AnalysisContext analysisContext) {
        EquipmentLineInfo equipmentLineInfo = EquipmentLineInfo.builder()
                .region(equipmentLineParam.getRegion())
                .name(equipmentLineParam.getBusName())
                .category(equipmentLineParam.getType())
                .voltageLevel(equipmentLineParam.getVoltageLevel())
                .currentLimit(equipmentLineParam.getCurrentLimit())
                .fileId(fileId)
                .userId(userId)
                .build();
        log.info("解析到设备线路数据: {}", equipmentLineInfo);
        dataList.add(equipmentLineInfo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        equipmentLineService.saveBatch(dataList);
        super.doAfterAllAnalysed(analysisContext);
        log.info("Sheet2数据插入完成！");
    }
}
